﻿using System;
using System.Reflection;
using Kinect.Recognition;
using Kinect.Recognition.Gestures;
using Kinect.Recognition.States;
using Kinect.Recognition.Tracking;
namespace Kinect
{
    public partial class KinectController
    {
        #region FSM with delegate


        public delegate void outRecordEventHandler(object sender, GestureRecordingEventArgs args);
        public delegate void outGestureRecognizedEventHandler(object sender, outGestureRecognizedEventArgs args);

        public event outRecordEventHandler outRecordingStarted;
        public event outRecordEventHandler outRecordingStarting;
        public event outRecordEventHandler outRecordingStopped;
        public event outRecordEventHandler outFramerecorded;
        public outGestureRecognizedEventHandler outGR;

        private void InitFSM(bool test)
        {
            string file = System.IO.Path.GetDirectoryName(new Uri(Assembly.GetAssembly(typeof(KinectController)).CodeBase).LocalPath);


            Configuration cfg;
            cfg = Configuration.Instance;
            if (!test)
            {
                cfg.GesturesFolder = file + @"\..\..\TestProject\Gestures\";
                cfg.InitializeFrom(file + @"\..\..\TestProject\Recoganizer\gesturesConfig.xml");
            }
            else
            {
                cfg.GesturesFolder = file + @"\..\..\Gestures\";
                cfg.InitializeFrom(file + @"\..\..\Recoganizer\gesturesConfig.xml");
            }

            context = cfg.CreateContext();
            fsm = new GesturesFSM<TrackingContext>(context);
            fsm.InitializeFromConfiguration(cfg);

            // bind to recording state events
            recordingState = (StateRecording)cfg.States[FSMStateId.Recording];
            recordingState.RecordingStarted += new GestureRecordingHandler(recordingState_RecordingStarted);
            recordingState.RecordingStarting += new GestureRecordingHandler(recordingState_RecordingStarting);
            recordingState.RecordingStopped += new GestureRecordingHandler(recordingState_RecordingStopped);
            recordingState.FrameRecorded += new GestureRecordingHandler(recordingState_FrameRecorded);

            // bind to fsm events
            fsm.GestureRecognized += new GestureRecognizedEventHandler(fsm_GestureRecognized);
            fsm.StateChanged += new StateChangedEventHandler(fsm_StateChanged);

        }
        private void fsm_StateChanged(object sender, Kinect.Recognition.States.StateChangedEventArgs args)
        {
        }

        private void fsm_GestureRecognized(object sender, GestureRecognizedEventArgs args)
        {

            if (this.outGR != null)
            {
                this.outGR(this, new outGestureRecognizedEventArgs(args.Gesture.Id.ToString())); ;
            }
            //AddMessage(string.Format("Gesture {0} recognized, distance {1}", , args.Gesture.MinDistance.ToString("F2")));
        }

        void recordingState_RecordingStopped(object sender, GestureRecordingEventArgs args)
        {
            Configuration.Instance.SaveGesture(args);
            if (this.outRecordingStopped != null)
            {
                this.outRecordingStopped(this, args);
            }

        }

        void recordingState_RecordingStarting(object sender, GestureRecordingEventArgs args)
        {
            recordingState.RecordingId = (GestureId)int.Parse(recordGesId);
            if (this.outRecordingStarting != null)
            {
                this.outRecordingStarting(this, args);
            }
        }

        private void recordingState_RecordingStarted(object sender, GestureRecordingEventArgs args)
        {

            if (this.outRecordingStarted != null)
            {
                this.outRecordingStarted(this, args);
            }
        }

        private void recordingState_FrameRecorded(object sender, GestureRecordingEventArgs args)
        {

            if (this.outFramerecorded != null)
            {
                this.outFramerecorded(this, args);
            }
        }
        #endregion
    }
}
